#define ElemType int
#include "common.h"

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target);

int main()
{
	int matrix[2][2] = { { 1, 2 }, { 3, 4 } };
	printf("%d\n", *(*matrix + 2));
	//int matrixColSize = 2;
	//searchMatrix(&matrix, 2, &matrixColSize, 4);
	return 0;
}

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target)
{
	//printf("%d\n", matrixSize);
	//printf("%d\n", *matrixColSize);
	//printf("%d\n", *((*matrix) + 1));
	// printf("%d\t%p\n", **matrix, *matrix);
	// printf("%d\t%p\n", **(matrix+1), *(matrix+1));
	// printf("%d\t%p\n", **(matrix+2), *(matrix+2));

	int row_size = (*matrixColSize);
	int total_size = matrixSize * row_size;

	int lo = 0, hi = total_size;
	while (lo < hi) {
		int mid = lo + (hi - lo >> 1);
		int col = mid / row_size, row = mid % row_size;
		int cur_value = matrix[col][row];

		if (cur_value == target)
			return true;
		else if (cur_value > target)
			hi = mid;
		else
			lo = mid + 1;
	}

	return false;
}
